import random

import requests


def get_random_image():
    """随机获取一张Wallhaven图片URL"""
    url = "https://wallhaven.cc/api/v1/search"
    page_random = random.randint(1, 2000)
    headers = {
        "user-agent": "Mozilla/5.0",
        "accept": "text/html, */*; q=0.01",
        "x-requested-with": "XMLHttpRequest",
    }
    params = {
        "q": "id:37",
        "sorting": "random",
        "ref": "fp",
        "seed": "Z2awIa",
        "page": page_random
    }

    try:
        response = requests.get(url, headers=headers, params=params, timeout=10)
        response.raise_for_status()
        data = response.json()
        if not data.get('data'):
            return None
        random_image = random.choice(data['data'])
        return random_image['path']
    except:
        return None


print(get_random_image())